<template>
  <div>
    <el-card class="box-card">
      <el-form :model="form" :inline="true" label-width="100px">
        <el-form-item label="时间:">
          <el-date-picker v-model="form.createTime" type="datetimerange" placeholder="选择一个日期" format="YYYY/MM/DD HH:mm:ss" value-format="YYYY-MM-DD HH:mm:ss " />
        </el-form-item>
        <el-form-item label="客户姓名:">
          <el-input v-model="form.clientName" />
        </el-form-item>
        <el-form-item label="客户电话:">
          <el-input v-model="form.phone" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="box-card">
      <div class="button_div">
        <span class="Order_title">我的订单</span>
          <el-button
          type="primary"
          size="large"
          class="button_add"
          v-if="showAdd"
          @click="OpenDialog('新增')"
          
          >新增</el-button
        >
      </div>
      <el-table
        v-loading="loading"
        :data="tableData.list"
        :default-sort="{ prop: 'date', order: 'descending' }"
        style="width: 100%"
      >
        <el-table-column prop="createTime" label="订单创建时间" sortable width="160" />
        <el-table-column prop="clientName" label="客户姓名" width="100" />
        <el-table-column prop="phone" label="客户电话" width="160" />
        <el-table-column prop="partner" label="合作方" width="120" />
        <el-table-column prop="orderManager" label="客户经理" width="120" />
        <el-table-column prop="orderStatus" label="订单状态" width="160" />
        <el-table-column prop="reserveAmount" label="定金金额" width="120" />
        <el-table-column prop="amount" label="合同金额" width="120" />
        <el-table-column prop="delayed" label="是否延期" width="160">
          <template #default="scope">
            <div v-if="scope.row.delayed === '延期'" style="color: red">
              {{ scope.row.delayed }}
            </div>
            <div v-else>
              {{ scope.row.delayed }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="address" label="地址" width="120" />
        <el-table-column fixed="right" label="操作" width="250">
          <template #default="scope" v-if="!showSupplier">
            <el-button size="small" @click="handleEdit(scope.$index, scope.row, '编辑')"
              >详情</el-button
            >
            <el-popconfirm
              width="200px"
              confirm-button-text="是的"
              cancel-button-text="不"
              :icon="InfoFilled"
              icon-color="#626AEF"
              title="确认删除?"
              @confirm="confirmEvent(scope.$index, scope.row)"
              @cancel="cancelEvent"
            >
             
            </el-popconfirm>
            <el-button size="small" type="primary" @click="Next(scope.$index, scope.row, '下一步')"
              >下一步</el-button>
            <el-button size="small" type="danger" >申请延期</el-button>
          </template>
          <!--          下面是供应商操作菜单-->
          <template #default="scope" v-if="showSupplier">
            <el-button size="small" type="primary" @click="finish(scope.row)">完成订单</el-button>
            <el-button size="small" type="primary" @click="Lookcontent(scope.$index, scope.row, '查看内容')">查看内容</el-button>
          </template>
        </el-table-column>
      </el-table>
      <template> </template>
      <el-pagination
        background
        :page-sizes="[1, 5, 10, 20]"
        layout="total, sizes, prev, pager, next"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :total="tableData.total"
      />
    </el-card>

    <DialogForm ref="dialogRef" @updateForm="updateForm" :orderId="orderid" />
  </div>
</template>
<script lang="ts" setup>
  import { reactive, ref } from 'vue';
  import {
    tbOrderDelete,
    tbOrderMyListData,
  } from '/@/api/tb/tbOrder';
  import { tbSupplierFinish } from '/@/api/tb/tbSupplierOrder';
  import { ElMessage } from 'element-plus';
  import DialogForm from './components/DialogForm.vue';
  import { InfoFilled } from '@element-plus/icons-vue';
  import { tbhistory } from '/@/api/tb/tb_get_history'
  import {useUserStore} from "../../../store/modules/user"
  const tableData = ref([]);
  const userstore=useUserStore().userInfo
  console.log(userstore);

  const showSupplier=ref(false)
  let orderid = reactive();
  const next_name=ref('')
  const dialogRef = ref<any>();
  const form = reactive({
    clientName: '',
    selectTime: '',
    phone: '',
    pageSize: 10,
    pageNo: 1,
  });
const showAdd=ref(false)
  const loading = ref(true);
  const getListData = async () => {
    const res = await tbOrderMyListData(form);
    if (res.list) {
      loading.value = false;
      tableData.value = res;
    }
  };
  getListData();
  const updateForm = () => {
    getListData();
  };
  const handleEdit = (index: number, row, title: string) => {
    dialogRef.value.Open(title, row.orderId);
  };
  const Next = async(index: number, row, title: string) => {
    const res = await tbhistory({orderId: row.orderId});
    for(let i = 0; i < res.list.length; i++) {
      if(res.list[i].finishStatus=="执行中"){
        next_name.value=res.list[i].executor
      }
    }
    if(next_name.value!=userstore?.userName){
      ElMessage.error("您不是此订单的执行人，禁止进行相关操作")
    }else{
      dialogRef.value.Open(title, row.orderId);
    }
  };
  const Lookcontent = (index: number, row, title: string) => {
    dialogRef.value.Open(title, row.orderId);
  };
  const confirmEvent = async (index: number, row) => {
    const res = await tbOrderDelete({ orderId: row.orderId });
    if (res.result) {
      ElMessage({
        message: '删除' + res.message,
        type: 'success',
      });
    } else {
      ElMessage.error('删除' + res.message);
    }
    getListData();
  };
  const cancelEvent = () => {
    ElMessage('取消删除');
  };

  const OpenDialog = (query) => {
    dialogRef.value.Open(query);
  };

  const onSubmit = async () => {
    getListData();
  };

  const handleSizeChange = (val: number) => {
    form.pageSize = val;
    getListData();
  };

  const handleCurrentChange = (val: number) => {
    form.pageNo = val;
    getListData();
  };

  // 获取到当前用户的角色信息
  const GetUserRole = async () => {
    for(let i=0;i<userstore?.roleList?.length;i++){
       if(userstore?.roleList[i].roleName=="供应商"){
        showSupplier.value=true;
       }
       if(userstore?.roleList[i].roleName=="销售主管"||userstore?.roleList[i].roleName=="销售人员"||userstore?.roleList[i].roleName=="设计主管")
       {
        showAdd.value=true;
       }
    }
  };

  GetUserRole();

  const finish = async (row) => {
    const res = await tbSupplierFinish(row);
    if (res.result) {
      ElMessage({
        message: res.message,
        type: 'success',
      });
      await getListData();
    } else {
      ElMessage.error(res.message);
    }
  };
</script>
<style lang="less" scoped>
  .box-card {
    margin-bottom: 20px;
    border-radius: 10px;
  }
  .button_div {
    width: 100%;
    display: flex;
    justify-content: space-between;
    .Order_title {
      padding: 5px 10px;
      font-size: 18px;
    }
  }
  .el-pagination {
    display: flex;
    justify-content: flex-end;
  }
</style>
